package com.cdu.controller;


import com.cdu.commons.MallConstants;
import com.cdu.commons.R;
import com.cdu.pojo.entity.Address;
import com.cdu.pojo.vo.UserInfoVO;
import com.cdu.pojo.vo.UserLoginVO;
import com.cdu.pojo.dto.TokenUser;
import com.cdu.pojo.dto.UserLoginDTO;
import com.cdu.pojo.dto.UserRegDTO;
import com.cdu.service.UserService;
import com.cdu.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/reg")
    public R<Void> register(@Validated UserRegDTO userRegDTO){
        log.info("接受的数据",userRegDTO);
        userService.reg(userRegDTO);
        return R.ok("注册成功");
    }

    @PostMapping("/log")
    public R<UserLoginVO> login(@Validated UserLoginDTO userLoginDTO){
        log.info("接受的数据是",userLoginDTO);
        UserLoginVO userLoginVO= userService.login(userLoginDTO);
        return new  R<UserLoginVO>().ok("登录成功",userLoginVO);
    }


    //获取用户信息
    @GetMapping("/info")
    public R<UserInfoVO> getUserInfo(HttpServletRequest request){

        String token=request.getHeader(MallConstants.TOKEN_SIGN);
        System.out.println("token");
        TokenUser tokenUser= JwtUtils.getTokenUser(token);
        log.info("tokenUser:{}",tokenUser);
        //获取用户信息的业务层
        UserInfoVO userInfoVO=userService.getUserInfo(tokenUser);

        return R.ok("成功",userInfoVO);
    }

    //修改用户信息接口
    @PutMapping("/info")
    public R<Void> updateUserInfo(UserInfoVO userInfoVO,HttpServletRequest request){
        //提取token并解析
        String token=request.getHeader(MallConstants.TOKEN_SIGN);
        TokenUser tokenUser=JwtUtils.getTokenUser(token);
        //打印
        log.info("当前用户是：{}",tokenUser);
        //修改用户信息的业务层
        userService.updateUserInfo(tokenUser,userInfoVO);

        return R.ok("修改成功");
    }




    //上传头像接口
    @PutMapping("/avtor")
    public R<String> updateAvtor(MultipartFile file,HttpServletRequest request){

        //提取token
        String token=request.getHeader(MallConstants.TOKEN_SIGN);
        //解析token
        TokenUser tokenUser=JwtUtils.getTokenUser(token);
        //打印
        log.info("当前用户是：{}",tokenUser);

        String url=userService.upload(file,tokenUser);


        return R.ok("上传成功",url);
    }

    //修改密码接口
    @PutMapping("/pwd")
    public R<Void> updatePwd(String oldPassword,String newPassword,String confirmPassword,HttpServletRequest request){
        //提取token
        String token=request.getHeader(MallConstants.TOKEN_SIGN);
        //解析token
        TokenUser tokenUser=JwtUtils.getTokenUser(token);
        //打印
        log.info("当前用户是：{}",tokenUser);
        //修改密码的业务层
        userService.updatePwd(tokenUser,oldPassword,newPassword);

        return R.ok("修改密码成功");

    }


    //获取用户地址接口
    @GetMapping("/address")
    public R<List<Address>> getUserAddress(HttpServletRequest request){
        //提取token
        String token=request.getHeader(MallConstants.TOKEN_SIGN);
        //解析token
        TokenUser tokenUser=JwtUtils.getTokenUser(token);
        //打印
        log.info("当前用户是：{}",tokenUser);
        //获取用户地址的业务层
        List<Address> addressList=userService.getUserAddress(tokenUser);

        return R.ok("获取成功",addressList);
    }


    }





